/**
 * Created by Tung An on 12/19/13.
 */

var ManagerLook = {

    isVisible: false,

    init: function () {
        if (!$class("username")[0] || !$class("username")[0].id || $class("username")[0].id != xmlUtils.getURLParameter("id")) {
            window.location = "/";
            return;
        }
        this.show();
        this.getListLook();
        this.isVisible = true;
    },

    onClick: function () {
        switch (event.target.data) {
            case "delete":
                var parentNode = getParentNode(event.target, "manage-item");
                this.deleteLook(parentNode);
                break;
            case "manage-item":
                var parentNode = getParentNode(event.target, "manage-item");
                window.location = "upload_image.jsp?look=" + parentNode.id + "&edit=true";
                break;
            default :
                break;
        }
    },

    deleteLook: function (node) {

        var id = parseInt(node.id);
        var success = function (resp) {
            node.remove();
        };

        var failure = function () {

        };

        ajax.post({
            url: "ProcessServlet",
            param: {action: "deleteLook", lookId: id},
            success: success,
            failure: failure
        });
    },

    getListLook: function () {
        var userId = parseInt($class("username")[0].id);
        var success = function (resp) {
            var html = xmlUtils.transformXSL(resp, "templates/manage_looks.xsl");
            $id('manage-look-list').innerHTML = "";
            $id('manage-look-list').appendChild(html);
            setTimeout(function () {
                GridFormat.refreshLayout("manage-item", $class("manage")[0], 30);
            }, 100);
        };

        var failure = function () {

        };

        ajax.post({
            url: "ProcessServlet",
            param: {action: "getManageLook", userId: userId},
            success: success,
            failure: failure
        });
    },

    show: function () {
        var listener = function (event) {
            var node = event.target;
            event.target.data = xmlUtils.getNodeWithData(node);
            ManagerLook.onClick(event);
        };
        $id("manage-page").addEventListener("click", listener, false);
    }
};
